package com.edge.pbx.view.eq;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.edge.pbx.view.common.VerticalSeekBar;

import com.edge.pbxdsp.R;
import com.hanlink.dsp.util.DspUtil;
import com.qztech.utils.YzDebug;

/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class EqChannelView extends ViewGroup implements SeekBar.OnSeekBarChangeListener, View.OnClickListener {
    private TextView mTvFreq, mTvValue;
    private VerticalSeekBar mVerticalSeekBar;
    private int mPageIndex, mFreqIndex;
    public EqChannelView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.layout_eq_item1, this);
        mTvFreq = findViewById(R.id.eq_item_freq);
        mTvValue = findViewById(R.id.eq_item_value);
        mVerticalSeekBar = findViewById(R.id.vSeekbar);
        mVerticalSeekBar.setOnSeekBarChangeListener(this);
    }

    public void setGainValue(int gain) {
        mVerticalSeekBar.setProgress(gain);
        YzDebug.d("Yang", "setGainValue:" + gain + " " + mVerticalSeekBar.getProgress() + " " + mFreqIndex);
        updateGainText();
    }

    public VerticalSeekBar getVerticalSeekBar() {
        return mVerticalSeekBar;
    }

    public void setFreqValue(int freqValue) {
        mTvFreq.setText(String.valueOf(freqValue));
    }

    public void setPageAndIndex(int page, int index) {
        mPageIndex = page;
        mFreqIndex = index;
    }

    public int getPageIndex() {
        return mPageIndex;
    }

    public int getFreqIndex() {
        return mFreqIndex;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
        final int useWidth = (int) (width * 0.8f);
        float textScale = 0.08f;
        float buttonScale = 0.10f;
        int buttonHeight = (int) (buttonScale * height);
        float textWHScale = 162f / 63f;
        int textHeight = (int) (height * textScale);
        int textWidth = (int) (textHeight * textWHScale);
        if (textWidth > useWidth) {
            textWidth = useWidth;
            textHeight = (int) (useWidth / textWHScale);
        }
        int vspace = (int) (height * 0.02f);

        View view = null;
        int specw = MeasureSpec.makeMeasureSpec(textWidth, MeasureSpec.EXACTLY);

        for (int i = 0, count = getChildCount(); i < count; i++) {
            view = getChildAt(i);
            if (view == null) continue;
            switch (view.getId()) {
                case R.id.eq_item_freq:
                case R.id.eq_item_value:
                    view.measure(specw, MeasureSpec.makeMeasureSpec(textHeight, MeasureSpec.EXACTLY));
                    break;
                case R.id.eq_item_add:
                case R.id.eq_item_sub:
                    view.measure(specw, MeasureSpec.makeMeasureSpec(buttonHeight, MeasureSpec.EXACTLY));
                    break;
                case R.id.seekBarContainer1:
                    view.measure(specw, MeasureSpec.makeMeasureSpec(height - (textHeight + buttonHeight + vspace) * 2, MeasureSpec.EXACTLY));
                    break;

            }
        }
    }


    private static int sTextSize;

    public static int getTextSize(TextView tv, int maxWidth, int maxHeight) {
        if (sTextSize == 0) {
            int textSize = (int) (maxHeight * 0.2f);
            int mh = (int) (maxHeight * 0.9f);
            int mw = (int) (maxWidth * 0.98f);

            Paint paint = new Paint();
            paint.setTextSize(textSize);
            String text = "22000";


            Rect rect = new Rect();
            paint.getTextBounds(text, 0, text.length(), rect);

            Log.d("Yang", "getTextSize textSize:" + textSize
                    + " mw:" + mw + " mh:" + mh
                    + " " + paint.measureText(text) + " h:" + rect.height() + " maxHeight:" + maxHeight);
            int count = 0;
            while (paint.measureText(text) > mw) {
                textSize--;
                paint.setTextSize(textSize);
                Log.d("Yang", "cal--------------11:" + textSize + " " + paint.measureText(text) + " " + mw);
                if (count++ > 100)
                    break;
            }

            count = 0;
            while (rect.height() > mh) {
                textSize--;
                paint.setTextSize(textSize);
                paint.getTextBounds(text, 0, text.length(), rect);
                Log.d("Yang", "cal--------------2:" + textSize);
                if (count++ > 100)
                    break;
                ;
            }
            sTextSize = textSize;
        }
        return sTextSize;
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        final int width = r - l;
        final int height = b - t;
//        Log.d("Yang", " -onLayout:" +width +" " + height);
        View view = null;
        int startLeft = 0;
        int freqHeight = 0;
        int buttonHeight = 0;
        int vspace = (int) (height * 0.02f);
        for (int i = 0, count = getChildCount(); i < count; i++) {
            view = getChildAt(i);
            if (view == null) continue;
            switch (view.getId()) {
                case R.id.eq_item_freq:
                    startLeft = (width - view.getMeasuredWidth()) / 2;
                    freqHeight = view.getMeasuredHeight();
                    view.layout(startLeft, 0, startLeft + view.getMeasuredWidth(), freqHeight);
                    break;
                case R.id.eq_item_add:
                    view.setOnClickListener(this);
                    buttonHeight = view.getMeasuredHeight();
                    t = freqHeight + vspace;
                    view.layout(startLeft, t, startLeft + view.getMeasuredWidth(), t + buttonHeight);
                    break;
                case R.id.seekBarContainer1:
                    t = freqHeight + buttonHeight + vspace;
                    view.layout(startLeft, t, startLeft + view.getMeasuredWidth(), t + view.getMeasuredHeight());
                    break;
                case R.id.eq_item_sub:
                    view.setOnClickListener(this);
                    b = height - freqHeight - vspace;
                    view.layout(startLeft, b - view.getMeasuredHeight(), startLeft + view.getMeasuredWidth(), b);
                    break;
                case R.id.eq_item_value:
                    view.layout(startLeft, height - view.getMeasuredHeight(), startLeft + view.getMeasuredWidth(), height);
                    break;


            }
        }
        int textSize = getTextSize(mTvFreq, width, freqHeight);
//        Log.d("Yang", "onFinishInflate textSize:"+textSize+" "+ width);
        if (textSize > 0) {
            mTvFreq.setTextSize(textSize);
            mTvValue.setTextSize(textSize);
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        mVerticalSeekBar.setAlpha(enabled?1.0f:0.4f);
        for(int i = 0 , len = getChildCount();i < len;i++){
            getChildAt(i).setEnabled(enabled);
        }
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);


        if (mFreqIndex == 0){
//            YzDebug.d("setSelected:"+ mFreqIndex +"- " + selected, new Throwable());
        }

//        mVerticalSeekBar.setSelected(selectedff);
    }

    private void updateGainText() {
        mTvValue.setText(DspUtil.gainToValue(mVerticalSeekBar.getProgress()));
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser && mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onProgressChanged(this, progress);
        }
        updateGainText();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onTrackingTouch(this, true);
        }

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        updateGainText();
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onTrackingTouch(this, false);
        }

    }

    private OnSeekBarChangeListener mOnSeekBarChangeListener;

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener onSeekBarChangeListener) {
        mOnSeekBarChangeListener = onSeekBarChangeListener;
    }

    public int getProgress() {
        return mVerticalSeekBar.getProgress();
    }

    @Override
    public void onClick(View v) {
        int progress = mVerticalSeekBar.getProgress();
        switch (v.getId()) {
            case R.id.eq_item_sub:
                progress--;
                break;
            case R.id.eq_item_add:
                progress++;
                break;
        }
        if (progress < 0 || progress > mVerticalSeekBar.getMax())
            return;
        if (progress != mVerticalSeekBar.getProgress()) {
            mVerticalSeekBar.setProgress(progress);
            if (mOnSeekBarChangeListener != null) {
                mOnSeekBarChangeListener.onTrackingTouch(this, false);
            }
        }

    }

    public interface OnSeekBarChangeListener {

        default void onProgressChanged(EqChannelView seekBar, int progress) {

        }

        default void onTrackingTouch(EqChannelView seekBar, boolean isStart) {

        }
    }
}
